﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Data.Design;
using System.Management;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class Police_Question_QuestionList : System.Web.UI.Page
{
    private bool Flag = true;
    private int PageCount = 15;
    private string ConstDeparmentID;
    protected void Page_Load(object sender, EventArgs e)
    {
        ConstDeparmentID = Yipusoft.Account.BLL.User.GetUser(User.Identity.Name).DepartmentID;
        if (!IsPostBack)
        {
            ListPageContral1.SetFirstPage();
            Bind(PageCount, 0);
            Flag = true;
            if (!ConstDeparmentID.Substring(4, 1).Equals("0"))
            {
                Response.Write("<script>alert('没有权限！');history.back(0);</script>");
                return;
            }
        }
    }

    public void ListPageContral1_PageSizeChange(int pageSize, int PageIndex)
    {
        Bind(pageSize, PageIndex);
    }

    private void Bind(int pageSize, int pageIndex)
    {

        QuestionDataContext db = new QuestionDataContext();
        bool bl = ConstDeparmentID.Substring(2, 3).Equals("000");

        if (Flag)
        {
            if (bl)
            {
                var result = (from s in db.SecuritySubject where s.CreateDepartment.Substring(5,1) == ConstDeparmentID.Substring(5,1)&&s.CreateDepartment.Substring(0,4)==ConstDeparmentID.Substring(0,4) && !(from o in db.Associate select o.SID).Contains(s.Id) orderby s.CreateTime ascending select s);
                if (result == null)
                    return;
                var obj = result.Skip(pageSize * pageIndex).Take(pageSize);                
                this.UserGrid.DataSource = obj;
                ListPageContral1.ItemCount = result.Count();
                this.UserGrid.DataBind();


            }
            else
            {
                var result = (from s in db.SecuritySubject where ((from aa in db.Associate where aa.DepartmentID.Substring(0, 6) == ConstDeparmentID.Substring(0, 6) && (!(from d in db.Examination where d.DepartmentID.Substring(0, 4) == ConstDeparmentID.Substring(0, 4) select d.SID).Contains(s.Id)) && aa.IsLevel == 0 select aa.SID).Contains(s.Id)) || (s.CreateDepartment == ConstDeparmentID && (!(from d in db.Examination where d.DepartmentID.Substring(0, 4) == ConstDeparmentID.Substring(0, 4) select d.SID).Contains(s.Id))) orderby s.CreateTime ascending select s);
                if (result == null)
                    return;
                var obj = result.Skip(pageSize * pageIndex).Take(pageSize);
                this.UserGrid.DataSource = obj;
                ListPageContral1.ItemCount = result.Count();
                this.UserGrid.DataBind();

            }
        }
        else
        {
            if (bl)
            {
                var obj = (from s in db.SecuritySubject where s.Caption.Contains(txtTitle.Text) && s.CreateDepartment.Substring(5, 1) == ConstDeparmentID.Substring(5, 1) && s.CreateDepartment.Substring(0, 4) == ConstDeparmentID.Substring(0, 4) && !(from o in db.Associate select o.SID).Contains(s.Id) orderby s.CreateTime ascending select s);
                if (obj == null)
                    return;
                var result = obj.Skip(pageSize * pageIndex).Take(pageSize);
                this.UserGrid.DataSource = result;
                ListPageContral1.ItemCount = obj.Count();
                this.UserGrid.DataBind();
            }
            else
            {
                var result = (from s in db.SecuritySubject where ((from aa in db.Associate where aa.DepartmentID.Substring(0, 6) == ConstDeparmentID.Substring(0, 6) && (!(from d in db.Examination where d.DepartmentID.Substring(0, 4) == ConstDeparmentID.Substring(0, 4) select d.SID).Contains(s.Id)) && aa.IsLevel == 0 select aa.SID).Contains(s.Id)) || (s.CreateDepartment == ConstDeparmentID && (!(from d in db.Examination where d.DepartmentID.Substring(0, 4) == ConstDeparmentID.Substring(0, 4) select d.SID).Contains(s.Id))) && s.Caption.Contains(txtTitle.Text) orderby s.CreateTime ascending select s);
                if (result == null)
                    return;
                var obj = result.Skip(pageSize * pageIndex).Take(pageSize);
                this.UserGrid.DataSource = obj;
                ListPageContral1.ItemCount = result.Count();
                this.UserGrid.DataBind();
            }
        }


    }
    protected void UserGrid_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            Label ID = (Label)e.Row.FindControl("ID");
            ID.Text = (((int)ListPageContral1.CurrentPageIndex) * ListPageContral1.PageSize + (e.Row.RowIndex + 1)).ToString();
            //当鼠标放上去的时候 先保存当前行的背景颜色 并给附一颜色 
            e.Row.Attributes.Add("onmouseover", "currentcolor=this.style.backgroundColor;this.style.backgroundColor='#F5F5F5',this.style.fontWeight='';");
            //当鼠标离开的时候 将背景颜色还原的以前的颜色 
            e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor=currentcolor,this.style.fontWeight='';");
        }
    }

    public string SubString(object obj, object len, object ID)
    {
        if (obj == null)
            return "";

        if (obj.ToString().Length > 30)
            return "<a href=ProblemList.aspx?ID=" + ID.ToString() + ">" + "".PadRight(2 * int.Parse(len.ToString()), '　') + obj.ToString().Substring(0, 30) + "..." + "</a>";
        else
            return "<a href=ProblemList.aspx?ID=" + ID.ToString() + ">" + "".PadRight(2 * int.Parse(len.ToString()), '　') + obj.ToString() + "</a>";
    }

    public string GetDepartmentName(object obj)
    {
        try
        {
            if (obj == null)
                return "";
            else
                return Yipusoft.Account.BLL.Department.GetDepartment(obj.ToString()).ShortName;
        }
        catch
        {
            return "";
        };


    }
    protected void lBtnEdit_Click(object sender, EventArgs e)
    {
        for (int i = 0; i < UserGrid.Rows.Count; i++)
        {
            CheckBox cb = (CheckBox)UserGrid.Rows[i].FindControl("CheckItem");
            if (cb.Checked)
            {
                if (Val(UserGrid.DataKeys[i].Value.ToString()))
                    return;

                Response.Redirect("QuestionNew.aspx?ID=" + UserGrid.DataKeys[i].Value.ToString());
                break;
            }
        }
        Prompt.Text = "警告：请选择记录";
    }
    protected void lBtnQuery_Click(object sender, EventArgs e)
    {
        //QuestionDataContext db = new QuestionDataContext();
        //var obj = (from o in db.SecuritySubject where o.Id == new Guid(Request["ID"]) select o).FirstOrDefault();
        //if (obj.CreateDepartment != ConstDeparmentID)
        //{
        //    lblPrompt.Text = "该试卷是由总队下达你不可以修改";
        //    return;

        //}
        if (txtTitle.Text.Trim() == "")
        {
            Flag = true;
            Response.Redirect("QuestionList.aspx");
        }
        else
        {
            Flag = false;
        }
        ListPageContral1.SetFirstPage();
        Bind(PageCount, 0);


    }
    protected void lBtnDelete_Click(object sender, EventArgs e)
    {
        QuestionDataContext db = new QuestionDataContext();
       
        for (int i = 0; i < UserGrid.Rows.Count; i++)
        {
            CheckBox cb = (CheckBox)UserGrid.Rows[i].FindControl("CheckItem");
            if (cb.Checked)
            {
                if (Val(UserGrid.DataKeys[i].Value.ToString()))
                    return;

                var obj1 = from s in db.SecuritySubject where s.Id == new Guid(UserGrid.DataKeys[i].Value.ToString()) select s;
                db.SecuritySubject.DeleteAllOnSubmit(obj1);
                db.SubmitChanges();

            }
        }
        ListPageContral1.SetFirstPage();
        Bind(PageCount, 0);

      

    }
    protected void lBtnAddCaption_Click(object sender, EventArgs e)
    {

        //for (int i = 0; i < UserGrid.Rows.Count; i++)
        //{
        //    CheckBox cb = (CheckBox)UserGrid.Rows[i].FindControl("CheckItem");
        //    if (cb.Checked)
        //    {
        //        Response.Redirect("QuestionNew.aspx?ID=" + UserGrid.DataKeys[i].Value.ToString());
        //    }
        //}
        Response.Redirect("QuestionNew.aspx");
    }
    protected void lBtnAddProblem_Click(object sender, EventArgs e)
    {
            
        for (int i = 0; i < UserGrid.Rows.Count; i++)
        {
            CheckBox cb = (CheckBox)UserGrid.Rows[i].FindControl("CheckItem");
            if (cb.Checked)
            {
                if (Val(UserGrid.DataKeys[i].Value.ToString()))
                    return;

                Response.Redirect("ProblemNew.aspx?ID=" + UserGrid.DataKeys[i].Value.ToString());
            }
        }
        Response.Redirect("ProblemNew.aspx");
    }

    protected void lbtnExamination_Click(object sender, EventArgs e)
    {
        string strID = string.Empty;
        lblPrompt.Text = "";
        QuestionDataContext db = new QuestionDataContext();
        for (int i = 0; i < UserGrid.Rows.Count; i++)
        {
            CheckBox cb = (CheckBox)UserGrid.Rows[i].FindControl("CheckItem");
            if (cb.Checked)
            {
                var result = db.SecurityProblem.Where(o => o.Sid == new Guid(UserGrid.DataKeys[i].Value.ToString())).FirstOrDefault();
                if (result == null)
                {
                    lblPrompt.Text = "您选择的标题没有试题，系统无法为您出卷！";
                    return;
                }
                strID += UserGrid.DataKeys[i].Value.ToString() ;
                if (ConstDeparmentID.Substring(2, 3).Equals("000"))
                {
                    Response.Redirect("ZongExamination.aspx?ID=" + strID + "&Check=" + rBtnSoldier.Checked.ToString());
                }
                else
                {
                    Response.Redirect("Examination.aspx?ID=" + strID);
                }
            }
        }

        //if (strID != string.Empty)
        //{
        //    Response.Redirect("Examination.aspx?ID=" + strID);
        //}
    }
    protected void rBtnCadre_CheckedChanged(object sender, EventArgs e)
    {
        Response.Redirect("~/Police/Question2/QuestionList.aspx");
    }

    private bool Val(string ID)
    {
        QuestionDataContext db1 = new QuestionDataContext();
        var obj = (from o in db1.SecuritySubject where o.Id == new Guid(ID) select o).FirstOrDefault();

        if (obj.CreateDepartment.Substring(0,6) != ConstDeparmentID.Substring(0,6))
        {
            lblPrompt.Text = "该试卷是由总队下达你不可以修改";
            return true;

        }
        return false;
    }
}
